package com.shdanyan.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.shdanyan.domain.sys.SysUserInfo;
import com.shdanyan.mapper.SysUserInfoMapper;
import com.shdanyan.service.UserInfoService;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private SysUserInfoMapper userInfoMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public List<SysUserInfo> getAll(SysUserInfo UserInfo) {
        if (UserInfo.getPage() != null && UserInfo.getRows() != null) {
            PageHelper.startPage(UserInfo.getPage(), UserInfo.getRows());
        }
        return userInfoMapper.selectAll();
    }

    @Override
    public SysUserInfo getById(Long id) {
        try {
            SysUserInfo user = userInfoMapper.selectByPrimaryKey((long) id);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void deleteById(Long id) {
        userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(SysUserInfo sysUserInfo) {

        if (sysUserInfo.getPassword().isEmpty()) {
            sysUserInfo.setPassword("123456");
        }
        sysUserInfo.setPassword(passwordEncoder.encode(sysUserInfo.getPassword()));

        if (sysUserInfo.getId() != null) {
            sysUserInfo.setPassword(sysUserInfo.getPassword());
            userInfoMapper.updateByPrimaryKey(sysUserInfo);
        } else {
            userInfoMapper.insert(sysUserInfo);
        }
    }

    @Override
    public SysUserInfo findByUsername(String username) {
        return userInfoMapper.findByUsername(username);
    }

}
